package com.dailyblue.java.spring.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

// Servlet API
@Controller
@RequestMapping("/third")
public class ThirdController {
    @PostMapping("/a")
    public String a(HttpServletRequest request) {
        int random = (int) (Math.random() * 100 + 1);
        request.setAttribute("A", "request:" + random);
        request.getSession().setAttribute("A", "session:" + random);
        request.getSession().getServletContext().setAttribute("A", "application:" + random);
        return "1";
    }

    @GetMapping("/b")
    public ModelAndView b(ModelAndView mv) {
        // 设置返回的模型
        Map model = mv.getModel();
        // 等同于request.setAttribute("B", "我是模型存放的内容");
        model.put("B", "我是模型存放的内容！");
        // 设置返回的视图
        mv.setViewName("1");
        return mv;
    }

    @GetMapping("/c")
    public String c() {
        System.out.println("cccc");
        return "redirect:/index.html";
    }
}
